package cmd

import (
	"fmt"
	"time"

	"code.alibaba-inc.com/agit/quarantine-helper/env"

	config "code.alibaba-inc.com/agit/quarantine-helper/config"
	utils "code.alibaba-inc.com/agit/quarantine-helper/utils"
	"github.com/spf13/cobra"
)

func displayOneCommit(commitID string) error {
	commit, err := utils.GetCommitByID(commitID, env.GetEnvsWithQuarantine())
	if err != nil {
		return err
	}

	if config.GetNoMerges() && len(commit.ParentIds) > 1 {
		return nil
	}

	committer := commit.Committer
	author := commit.Author

	fmt.Print(config.GetDelimiter())
	if config.GetCommitID() {
		fmt.Print(commit.Id)
	}
	fmt.Print(config.GetDelimiter())
	if config.GetCommitMessage() {
		fmt.Print(commit.Body)
	}
	//committer
	fmt.Print(config.GetDelimiter())
	if config.GetCommitterName() {
		fmt.Print(committer.Name)
	}
	fmt.Print(config.GetDelimiter())
	if config.GetCommitterEmail() {
		fmt.Print(committer.Email)
	}
	fmt.Print(config.GetDelimiter())
	if config.GetCommitTime() {
		fmt.Print(committer.Date.Format(time.RFC3339))
	}
	fmt.Print(config.GetDelimiter())
	//author
	if config.GetAuthorName() {
		fmt.Print(author.Name)
	}
	fmt.Print(config.GetDelimiter())
	if config.GetAuthorEmail() {
		fmt.Print(author.Email)
	}
	fmt.Print(config.GetDelimiter())
	if config.GetAuthoredTime() {
		fmt.Print(author.Date.Format(time.RFC3339))
	}
	fmt.Println(config.GetDelimiter())
	return nil
}

func displayCommits(commitIDArray []string) error {
	if len(commitIDArray) <= 0 {
		return nil
	}

	for _, commitID := range commitIDArray {
		if err := displayOneCommit(commitID); err != nil {
			return err
		}
	}
	return nil
}

var displayCmd = &cobra.Command{
	Use:   "display",
	Short: "display detailed info of commits.",
	Long:  `display detailed info of commits that are in Quarantine but not in the repository.`,
	RunE: func(cmd *cobra.Command, args []string) error {
		oids, err := utils.GetCommitIDArray(nil)
		if err != nil {
			return err
		}
		return displayCommits(oids)
	},
}

func init() {
	rootCmd.AddCommand(displayCmd)
	displayCmd.Flags().BoolVarP(&config.CommitInfo.ID, "commitID", "i", false, "diplay commit id.")
	displayCmd.Flags().BoolVarP(&config.CommitInfo.Message, "message", "m", false, "diplay commit message.")
	//committer
	displayCmd.Flags().BoolVarP(&config.CommitInfo.CommittedTime, "committedTime", "", false, "diplay commit time.")
	displayCmd.Flags().BoolVarP(&config.CommitInfo.CommitterName, "committerName", "", false, "diplay committer name.")
	displayCmd.Flags().BoolVarP(&config.CommitInfo.CommitterEmail, "committerEmail", "", false, "diplay committer email.")
	//author
	displayCmd.Flags().BoolVarP(&config.CommitInfo.AuthoredTime, "authoredTime", "", false, "diplay authored time.")
	displayCmd.Flags().BoolVarP(&config.CommitInfo.AuthorName, "authorName", "", false, "diplay author name.")
	displayCmd.Flags().BoolVarP(&config.CommitInfo.AuthorEmail, "authorEmail", "", false, "diplay author email.")

	displayCmd.Flags().StringVarP(&config.Delimiter, "delimiter", "d", "-088084-", "delimiter to separate output.")

	// whether to show merge commit
	displayCmd.Flags().BoolVarP(&config.NoMerges, "no-merges", "", false, "do not show merge commit")
}
